package com.fcat.common.user.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.fcat.base.common.util.DateUtil;
import com.fcat.springboot.mybatis.util.JsonUtil;
import com.fcat.common.user.vo.SysLoginLog;
import com.fcat.common.user.vo.UserAuditVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import com.fcat.springboot.mybatis.controller.TsuantoneBaseController;
import com.fcat.common.user.service.UserAuditService;
import com.fcat.common.user.entity.UserAudit;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("userAudit")
public class UserAuditController extends TsuantoneBaseController<UserAuditService,UserAudit,Integer> {


    @ApiOperation(value = "3.6.9.获取用户信息", notes = "")
    @RequestMapping(value = "/getUserAuditList",method = RequestMethod.GET)
    public JSONObject getUserAuditList(@RequestParam("username") String username,
                                       @RequestParam("optRecord")String optRecord,
                                       @RequestParam("startTime")Long startTime,
                                       @RequestParam("endTime")Long endTime,
                                       @RequestParam("pageSize") Integer pageSize,
                                       @RequestParam("pageNo") Integer pageNo) {

        if (pageSize == null || pageNo == null) {
            pageSize =20;
            pageNo = 1;
        }
        PageHelper.startPage(pageNo, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("optRecord", optRecord);
        if (startTime != null && !startTime.equals(0)) {
            Date startDete = new Date(startTime);
            map.put("startTime", startDete);
        }
        if (endTime != null && !endTime.equals(0)) {
            Date endDete = new Date(endTime);
            map.put("endTime", endDete);
        }

        List<UserAudit> userList = bsi.getUserAuditList(map);
        PageInfo<UserAudit> pageInfo = new PageInfo(userList);
        JSONArray jsonArray = new JSONArray();

        if (!CollectionUtils.isEmpty(userList)) {
            jsonArray = JSONArray.parseArray(JSONArray.toJSONString(userList));
        }
        JSONObject resultJson = new JSONObject();

        resultJson.put("pageCount", pageInfo.getPages());
        resultJson.put("total", pageInfo.getTotal());
        resultJson.put("list",jsonArray);
        return JsonUtil.getSuccessJsonObject(resultJson);
    }

    @ApiOperation("3.18.2  通过id查询用户审计详情")
    @GetMapping("/getOneByUserAuditId")
    public JSONObject getOneByUserAuditId(@RequestParam("userAuditId") String userAuditId) {
        UserAudit UserAudit = bsi.getOneByUserAuditId(userAuditId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", UserAudit.getId());
        jsonObject.put("area", UserAudit.getArea());
        jsonObject.put("createTime", UserAudit.getCreateTime());
        jsonObject.put("optRecord", UserAudit.getOptRecord());
        jsonObject.put("ip", UserAudit.getIp());
        jsonObject.put("username", UserAudit.getUsername());
        return JsonUtil.getSuccessJsonObject(jsonObject);
    }
    @ApiOperation(value = "3.18.3.获取系统登录日志", notes = "")
    @GetMapping(value = "/getSysLoginLogList")
    public JSONObject getSysLoginLogList( @RequestParam("pageSize") Integer pageSize, @RequestParam("pageNo") Integer pageNo,
                                          String name,Long startTime,Long endTime) {
        Map<String,Object> map=new HashMap<>();
        map.put("name",name);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        PageHelper.startPage(pageNo, pageSize);
        List<SysLoginLog> sysLoginLogList= bsi.getSysLoginLogList(map);
        PageInfo<SysLoginLog> pageInfo = new PageInfo(sysLoginLogList);

        JSONObject returnJson = new JSONObject();
        returnJson.put("pageCount", pageInfo.getPages());
        returnJson.put("total", pageInfo.getTotal());
        returnJson.put("list", sysLoginLogList);
        return JsonUtil.getSuccessJsonObject(returnJson);
    }


    @ApiOperation(value = "3.18.4.获取日志列表（组织机构管理，用户日志管理，被监控人管理，设备信息管理，监控任务管理，审批）", notes = "")
    @GetMapping(value = "/getLogList")
    public JSONObject getLogList(@RequestParam("logType") String logType,@RequestParam("pageSize") Integer pageSize, @RequestParam("pageNo") Integer pageNo,
                                 String optObject,String optType,String name,Long startTime,Long endTime) {
        PageHelper.startPage(pageNo, pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("optObject",optObject);
        map.put("logType",logType);
        map.put("optType",optType);
        map.put("name",name);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        List<UserAuditVO> logList= bsi.getLogList(map);
        PageInfo<UserAudit> pageInfo = new PageInfo(logList);

        JSONObject returnJson = new JSONObject();
        returnJson.put("pageCount", pageInfo.getPages());
        returnJson.put("total", pageInfo.getTotal());
        if(CollectionUtils.isEmpty(logList)){
            returnJson.put("list", null);
        }else{
            JSONArray jsonArray=new JSONArray();
            for (UserAuditVO userAudit : logList) {
                JSONObject jsonObject=new JSONObject();
                jsonObject.put("optObject",userAudit.getOptObject());
                jsonObject.put("optType",userAudit.getOptType());
                jsonObject.put("username",userAudit.getUsername());
                jsonObject.put("name",userAudit.getName());
                jsonObject.put("createTime",userAudit.getCreateTime());
                jsonObject.put("content",userAudit.getContent());
                jsonArray.add(jsonObject);
            }
            returnJson.put("list", jsonArray);
        }
        return JsonUtil.getSuccessJsonObject(returnJson);
    }


}